<?php
session_start();
include './c/config.php';
header('Content-Type: text/html; charset=utf-8');
$f='';
if (isset($_GET['f'])){
    $f=$_GET['f'];
}
$_SESSION['friendOp']=$f;
 
//$url='http://127.0.0.1:8031/api/wx/access_token?appId='.$appId.'&code='.$_GET['code'];
 

$access_token = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appId.'&secret='.$secret.'&code='.$_GET['code'].'&grant_type=authorization_code';
$access = file_get_contents($access_token);
$accessObj = json_decode($access);
$access_token = $accessObj->access_token;
var_dump($access_token);
$openid   = $accessObj->openid;
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
$theData = json_decode(file_get_contents($user_info_url), true);

 

//$getData=json_decode(file_get_contents($url), true);
$openid=$getData['data']['openid'];
//$access_token=$getData['data']['access_token'];
//$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
//$theData=json_decode(file_get_contents($url), true);
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
$_SESSION['openid']=$openid;

function filterEmoji($str)
{
    $str = preg_replace_callback(
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str);
    
    return $str;
}

$sql="select id from ".$tableVer."_users where openid='".$openid."'  limit 0,1 ";
$query = $mysqli->query($sql);
if($query->num_rows>0){
}else{
    
    $name=filterEmoji(addslashes(trim($theData['nickname'])));
    $sql = "insert into ".$tableVer."_users(name,openid,nickname,headimg,province,city,createtime) values ('".$name."','".$openid."','".base64_encode($theData['nickname'])."', '".$theData['headimgurl']."','".$theData['province']."','".$theData['city']."', '".date("Y-m-d H:i:s")."' )";
    $mysqli->query($sql);
}
 

header('Location: http://climb-game.51wom.com/');
exit;
